package com.uenpay.oem.ui.auth;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.socks.library.KLog;
import com.uenpay.im.utilities.Toasts;
import com.uenpay.oem.R;
import com.uenpay.oem.data.bean.UserIdCardInfo;
import com.uenpay.util.app.base.BaseFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Author: liulou
 * Date: 2017/6/16.
 * desc:
 */

public class UploadIdCardFragment extends BaseFragment {

    private static final String TAG = "UploadIdCardFragment";

    @BindView(R.id.fl_take_photo_positive)
    FrameLayout flTakePhotoPositive;
    @BindView(R.id.fl_take_photo_back)
    FrameLayout flTakePhotoBack;
    @BindView(R.id.iv_take_photo_cover_positive)
    ImageView ivTakePhotoCoverPositive;
    @BindView(R.id.tv_take_photo_tip_positive)
    TextView tvTakePhotoTipPositive;
    @BindView(R.id.iv_take_photo_cover_back)
    ImageView ivTakePhotoCoverBack;
    @BindView(R.id.tv_take_photo_tip_back)
    TextView tvTakePhotoTipBack;
    @BindView(R.id.tv_take_photo_requirement)
    TextView tvTakePhotoRequirement;
    @BindView(R.id.btn_next)
    Button btnNext;

    private Unbinder mUnbinder;
    private static AuthFragmentChangeCallback mCallBack;


    //身份证正反面是否验证通过
    private boolean isIdCardPositivePass;
    private boolean isIdCardBackPass;

    private UserIdCardInfo mUserIdCardInfo = null;

    @Override
    protected void onCreateView(Bundle savedInstanceState) {
        super.onCreateView(savedInstanceState);
        setContentView(R.layout.fragment_auth_upload_idcard);
        mUnbinder = ButterKnife.bind(this, getContentView());
        SpannableString spannableString = new SpannableString(tvTakePhotoRequirement.getText());
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.background_accent)), 8, spannableString.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        tvTakePhotoRequirement.setText(spannableString);
    }

    public static UploadIdCardFragment newInstance(AuthFragmentChangeCallback callback) {
        mCallBack = callback;
        return new UploadIdCardFragment();
    }

    @OnClick({R.id.fl_take_photo_positive, R.id.fl_take_photo_back})
    public void takePhotoPositive(View view) {
        Intent it = new Intent(getActivity(), AuthCameraActivity.class);
        it.putExtra("type", getTakePhotoType(view.getId()));
        startActivityForResult(it, Activity.RESULT_FIRST_USER);
    }

    @OnClick(R.id.btn_next)
    public void nextClick() {
        if (!isIdCardPositivePass || !isIdCardBackPass) {
            Toasts.shortToast(getActivity(), "请先上传身份证信息");
            return;
        }
        if (mUserIdCardInfo == null) {
            resetPhotoStatus();
            return;
        }
        if (mCallBack != null) mCallBack.uploadIdCardSuccess(mUserIdCardInfo);
    }

    private int getTakePhotoType(int id) {
        if (id == R.id.fl_take_photo_back) {
            return AuthCameraActivity.TAKE_PHOTO_BACK;
        } else {
            return AuthCameraActivity.TAKE_PHOTO_POSITIVE;
        }
    }

    /**
     * 变更照相状态
     */
    private void changePhotoStatus(int type) {
        if (type < 0) return;
        if (type == AuthCameraActivity.TAKE_PHOTO_POSITIVE) {
            ivTakePhotoCoverPositive.setImageResource(R.drawable.pic_camera_cover_success);
            tvTakePhotoTipPositive.setText(R.string.idcard_recognition_success);
            flTakePhotoPositive.setEnabled(false);
            isIdCardPositivePass = true;
        } else if (type == AuthCameraActivity.TAKE_PHOTO_BACK) {
            ivTakePhotoCoverBack.setImageResource(R.drawable.pic_camera_cover_success);
            tvTakePhotoTipBack.setText(R.string.idcard_recognition_success);
            flTakePhotoBack.setEnabled(false);
            isIdCardBackPass = true;
        }
    }

    /**
     * 数据异常时重置拍照状态
     */
    private void resetPhotoStatus() {
        ivTakePhotoCoverPositive.setImageResource(R.drawable.pic_camera_cover);
        tvTakePhotoTipPositive.setText(R.string.upload_IDcard_positive);
        flTakePhotoPositive.setEnabled(true);
        ivTakePhotoCoverBack.setImageResource(R.drawable.pic_camera_cover);
        tvTakePhotoTipBack.setText(R.string.upload_IDcard_back);
        flTakePhotoBack.setEnabled(true);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            KLog.d("照片识别成功！");
            if (data != null) {
                int type = data.getIntExtra("type", -1);
                if (type == AuthCameraActivity.TAKE_PHOTO_POSITIVE) {
                    if (data.getParcelableExtra("idcard") != null) {
                        mUserIdCardInfo = data.getParcelableExtra("idcard");
                        KLog.d(TAG, mUserIdCardInfo.toString());
                    }
                }
                changePhotoStatus(type);
            }

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mCallBack != null) {
            mCallBack = null;
        }
        mUnbinder.unbind();
    }
}
